package com.woniuxy.controller;

import com.woniuxy.anno.PreAuthorize;
import com.woniuxy.entity.dto.WaybillDTO;
import com.woniuxy.entity.dto.YiDTO;
import com.woniuxy.service.YiService;
import com.woniuxy.util.MinioUtils;
import com.woniuxy.util.PageData;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: tsm
 * @description: 异常监控controller层
 * @create: 2024-10-18 14:43
 **/
@RestController
@RequestMapping("/exception")
public class YiController {
    @Resource
    private YiService yiService;
    @Resource
    private MinioUtils minioUtils;

    @PostMapping("/queryException")
    public ResponseData<?> queryException(@RequestBody PageData<YiDTO> pageData) {
        return yiService.queryException(pageData);
    }

    @PostMapping("/queryExceptionT")
    public ResponseData<?> queryExceptionT(@RequestBody PageData<YiDTO> pageData){
        return yiService.queryET(pageData);
    }

    @PostMapping("/add")
    public ResponseData<?> add(@RequestBody YiDTO yiDTO) {
        return yiService.add(yiDTO);
    }

    @PostMapping("/addIdByImg")
    public ResponseData<?> addIdByImg(@RequestBody YiDTO yiDTO){
        return yiService.addIdByImg(yiDTO);
    }

    @GetMapping("/queryById/{id}")
    public ResponseData<?> queryById(@PathVariable("id") String id) {
        return yiService.queryById(id);
    }

    @PostMapping("/updateByFinish")
    @PreAuthorize(value = {"finish"})
    public ResponseData<?> updateByFinish(@RequestBody YiDTO yiDTO) {
        return yiService.updateByFinish(yiDTO);
    }

    @GetMapping("/delete/{id}")
    public ResponseData<?> delete(@PathVariable("id") String id) {
        return yiService.delete(id);
    }

    @PostMapping("/update")
    public ResponseData<?> update(@RequestBody YiDTO yiDTO) {
        return yiService.update(yiDTO);
    }

    @PostMapping("/batchDelete")
    public ResponseData<?> batchDelete(@RequestBody List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return new ResponseData<>().fail(ResponseEnum.FAIL);
        }
        return yiService.batchDelete(ids);
    }

    @GetMapping("/queryOthersById/{id}")
    public ResponseData<?> queryOthersById(@PathVariable("id") String id) {
        return yiService.queryOthersById(id);
    }

    @PostMapping("/saveData")
    public ResponseData<?> saveData(@RequestBody YiDTO yiDTO) {
        return yiService.saveData(yiDTO);
    }

    @GetMapping("/export")
    public ResponseData<?> exportYiData(HttpServletResponse response){
        return yiService.exportYiData(response);
    }

    @PostMapping("/importData")
    public ResponseData<?> importYiData(@RequestParam("file") MultipartFile file){
        return yiService.importYiData(file);
    }

    @PostMapping("/queryWall")
    public ResponseData<?> queryWall(@RequestBody PageData<WaybillDTO> pageData){
        return yiService.queryByWaybillDTO(pageData);
    }

    @GetMapping("/queryByWaybillId/{id}")
    public ResponseData<?> queryByWaybillId(@PathVariable("id") String id){
        return yiService.queryWaybillById(id);
    }

    @PostMapping("/updateImg")
    public ResponseData<?> updateImg(@RequestBody YiDTO yiDTO){
        return yiService.updateImg(yiDTO.getId(),yiDTO.getImages());
    }

    /**
     * 头像上传解决
     * 怎么接收（图片）文件类型数据
     * MultipartFile:接收文件类型数据
     */
    @PostMapping("/imgLoad")
    public ResponseData<?> uploadImg(MultipartFile file){
        try {
            return minioUtils.uploadFile(file, "img");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("上传失败");
        }
    }
}